import { async } from "../../lib/runtime/runtime";
import { request } from "../../request/index"
//Page Object
Page({
  data: {
    tabs: [
      { id: 0, name: "综合", isactive: true },
      { id: 1, name: "销量", isactive: false },
      { id: 2, name: "价格", isactive: false }
    ],
    shoplist: []
  },
  total: 0,//总条数
  totalpage: 0,//总页数
  queryinfo: {//发送请求需要的参数
    query: '',
    cid: "",
    pagenum: 1,
    pagesize: 20
  },
  onLoad: function (options) {
    console.log(options)
    this.getshoplist()
    this.queryinfo.cid = options.cat_id || "";
    this.queryinfo.query = options.query || ""
  },
  onReachBottom() {//页面上滑，滚动条触底事件
    if (this.queryinfo.pagenum >= this.totalpage) {
      wx.showToast({
        title: '没有下一页数据了',
      });
    } else {
      this.queryinfo.pagenum = this.queryinfo.pagenum + 1
      this.getshoplist()
    }
  },

  indexchange(e) {
    const { index } = e.detail
    let { tabs } = this.data
    tabs.forEach((v, i) => i === index ? v.isactive = true : v.isactive = false)
    this.setData({
      tabs
    })
  },
  async getshoplist() {//发送请求
    const res = await request({ url: 'goods/search', data: this.queryinfo })
    this.total = res.data.message.total
    this.totalpage = Math.ceil(this.total / this.queryinfo.pagesize)
    console.log(this.totalpage)
    console.log(this.total)
    this.setData({
      shoplist: res.data.message.goods
    })
  },
  //触发下拉刷新的事件
  onPullDownRefresh() {
    this.queryinfo.pagenum = 1
    this.getshoplist()
  },


});
